package com.tensquare.qa.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tensquare.qa.dao.ReplyDao;
import com.tensquare.qa.pojo.Reply;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ReplyService {
    @Autowired
    private IdWorker idWorker;
   @Autowired
   private ReplyDao replyDao;
   @Autowired
   private HttpServletRequest request;
    public void save(Reply reply) {
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        if(claims_user==null){
            throw  new RuntimeException("权限不足");
        }
        reply.setId(idWorker.nextId()+"");
        replyDao.insert(reply);
    }

    public List<Reply> findAll() {
        return replyDao.selectList(null);

    }

    public Reply findById(String replyId) {
        return replyDao.selectById(replyId);
    }

    public void updateByReplyId(Reply reply) {
        replyDao.updateById(reply);
    }

    public void deleteByReplyId(String replyId) {
        replyDao.deleteById(replyId);
    }

    public Page<Reply> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<Reply> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        Page<Reply> page1=new Page<>(page,size);
        List<Reply> replys = replyDao.selectPage(page1, wrapper);
        page1.setRecords(replys);
        return page1;
    }

    public List<Reply> findBySearch(Map<String, Object> map) {
        return replyDao.selectByMap(map);
    }

    public List<Reply> findByProblemId(String problemId) {
        Map<String,Object> map=new HashMap<>();
        List<Reply> replies = replyDao.selectByMap(map);
        return replies;
    }
}
